package cn.xyy.system.api;

import cn.xyy.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xyy.common.domain.system.auth.Dict;
import cn.xyy.common.domain.system.auth.DictItem;
import cn.xyy.common.domain.system.auth.DictItemVO;
import cn.xyy.common.domain.system.auth.DictVO;
import cn.xyy.common.annotation.log.Log;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.xyy.common.algorithm.DictAlgorithm;
import cn.xyy.common.domain.mvc.R;

/**
 * 数据字典
 * 
 * @author xyy
 *
 */
@RestController
@RequestMapping(value = "/rest/dict")
public class DictRest {

	@Autowired
	private DictService dictService;

	@RequestMapping(value = "getDictItemList")
	public R getDictItemList(@RequestParam("dictName") String dictName) {
		return R.ok(dictService.getDictItemList(dictName));
	}

	@RequestMapping(value = "getDictAllList")
	public R getDictAllList() {
		return R.ok(DictAlgorithm.tree(dictService.getDictAllList()));
	}

	@Log("新增字典")
	@SaCheckPermission("dict:add")
	@RequestMapping(value = "addDict", method = RequestMethod.POST)
	public R addDict(@RequestBody Dict dict) {
		dictService.addDict(dict);
		return R.ok();
	}

	@Log("修改字典")
	@SaCheckPermission("dict:edit")
	@RequestMapping(value = "updateDict", method = RequestMethod.POST)
	public R updateDict(@RequestBody DictVO vo) {
		dictService.updateDict(vo);
		return R.ok();
	}

	@Log("删除字典")
	@SaCheckPermission("dict:del")
	@RequestMapping(value = "deleteDict")
	public R deleteDict(@RequestParam("id") String id) {
		dictService.deleteDict(id);
		return R.ok();
	}

	// -------------------------------------------

	@RequestMapping(value = "getDictItemListPage")
	public R getDictItemPage(DictItemVO vo) {
		return R.ok(dictService.getDictItemPage(vo));
	}

	@Log("新增字典项")
	@SaCheckPermission("dict:add")
	@RequestMapping(value = "addDictItem", method = RequestMethod.POST)
	public R addDictItem(@RequestBody DictItem item) {
		dictService.addDictItem(item);
		return R.ok();
	}

	@Log("修改字典项")
	@SaCheckPermission("dict:edit")
	@RequestMapping(value = "updateDictItem", method = RequestMethod.POST)
	public R updateDictItem(@RequestBody DictItemVO vo) {
		dictService.updateDictItem(vo);
		return R.ok();
	}

	@Log("删除字典项")
	@SaCheckPermission("dict:del")
	@RequestMapping(value = "deleteDictItem")
	public R deleteDictItem(@RequestParam("id") String id) {
		dictService.deleteDictItem(id);
		return R.ok();
	}

}
